package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.Shopcar;
import cn.duli.xianer.service.GoodsService;
import cn.duli.xianer.service.ShopcarService;
import com.alipay.api.domain.Shop;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shopcar")
public class ShopcarController {

    @Autowired
    private ShopcarService shopcarService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加购物车
     * 1.如果当前用户的购物车中之前就已经有这个对应的商品了，就需要提示用户此商品已存在于购物车中
     * 2.如果没有，直接添加商品到购物车中
     * @param shopcar
     * @return
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult save(Shopcar shopcar) {
        Shopcar shopcarOne = shopcarService.findByGidAndUid(shopcar.getsUid(),shopcar.getsGid());
        shopcar.setsPrice(goodsService.findById(shopcar.getsGid()).getgPrice());
        shopcar.setsFire(goodsService.findById(shopcar.getsGid()).getgFire());
        shopcar.setsTotal(shopcar.getsPrice().add(shopcar.getsFire()));
        if (shopcarOne==null) {
            int sId = shopcarService.save(shopcar);
            Shopcar shopcar1 = shopcarService.findById(sId);
            return new MyResult(200, "成功添加至购物车",null,null,shopcar1,null);
        } else {
            return new MyResult(401, "购物车中已存在此商品",null,null,null,null);
        }
    }

    /**
     * 根据用户id获取该用户的购物车商品列表
     * @param uId
     * @return
     */
    @GetMapping("/findByUserId/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findByUserId(@PathVariable("uId") Integer uId) {
        List<Shopcar> shopcarList = shopcarService.findByUserId(uId);
        return new MyResult(200, "根据用户id获取该用户的购物车商品列表",null,shopcarList,null,null);
    }

    /**
     * 根据购物车id单个选中此条购物车（修改商品为选中状态）
     * @param sId
     * @return
     */
    @RequestMapping("/updateChecked/{sId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateChecked(@PathVariable("sId") Integer sId) {
        shopcarService.updateChecked(sId);
        return new MyResult(200, "根据购物车id单个选中此条购物车",null,null,null,null);
    }

    /**
     * 根据购物车id取消单个选中此条购物车（修改商品为未选中状态）
     * @param sId
     * @return
     */
    @RequestMapping("/updateNotCheck/{sId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateNotCheck(@PathVariable("sId") Integer sId) {
        shopcarService.updateNotCheck(sId);
        return new MyResult(200, "根据购物车id取消单个选中此条购物车",null,null,null,null);
    }

    /**
     * 根据用户id全选购物车列表
     * @param uId
     * @return
     */
    @RequestMapping("/updateCheckedAll/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateCheckedAll(@PathVariable("uId") Integer uId) {
        List<Shopcar> list = shopcarService.findByUserId(uId);
        for (int i=0;i<list.size();i++) {
            shopcarService.updateChecked(list.get(i).getsId());
        }
        return new MyResult(200, "根据用户id全选购物车列表",null,null,null,null);
    }

    /**
     * 根据用户id取消全选购物车列表
     * @param uId
     * @return
     */
    @RequestMapping("/updateNotCheckAll/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateNotCheckAll(@PathVariable("uId") Integer uId) {
        List<Shopcar> list = shopcarService.findByUserId(uId);
        for (int i=0;i<list.size();i++) {
            shopcarService.updateNotCheck(list.get(i).getsId());
        }
        return new MyResult(200, "根据用户id取消全选购物车列表",null,null,null,null);
    }

    /**
     * 根据购物车id删除此条购物车
     * @param sId
     * @return
     */
    @RequestMapping("/deleteById/{sId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteById(@PathVariable("sId") Integer sId) {
        shopcarService.deleteById(sId);
        return new MyResult(200, "根据购物车id删除此条购物车",null,null,null,null);
    }

    /**
     * 根据用户id获取用户购物车商品数量
     * @param uId
     * @return
     */
    @GetMapping("/findCountByUserId/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findCountByUserId(@PathVariable("uId") Integer uId) {
        int count = shopcarService.findCountByUserId(uId);
        return new MyResult(200, "根据用户id获取用户购物车商品数量",null,null,count,null);
    }

    /**
     * 获取用户选中的商品数量
     * @param uId
     * @return
     */
    @GetMapping("/findCheckCountByUserId/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findCheckCountByUserId(@PathVariable("uId") Integer uId) {
        int count = shopcarService.findCheckCountByUserId(uId);
        return new MyResult(200, "获取用户选中的商品数量",null,null,count,null);
    }

    /**
     * 根据用户id计算用户选中的商品小计总价
     * @param uId
     * @return
     */
    @GetMapping("/findTotalPrice/{uId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findTotalPrice(@PathVariable("uId") Integer uId) {
        List<Shopcar> list = shopcarService.findGoodsByUserId(uId);
        // 解决计算商品价格时丢失计算精度问题，使用Java自带的BigDecimal类的String构造器
        BigDecimal totalPrice = BigDecimal.ZERO;
        List<BigDecimal> decimals = new ArrayList<>();
        for (int i=0;i<list.size();i++) {
            decimals.add(list.get(i).getsTotal());
        }
        for (BigDecimal big:decimals) {
            totalPrice = totalPrice.add(big);
        }
        return new MyResult(200, "根据用户id计算用户选中的商品总价",null,null,totalPrice,null);
    }

    /**
     * 根据用户id获取用户选中的商品列表
     * @param uId
     * @return
     */
    @GetMapping("/findGoodsByUserId/{uId}")
    @RequiresRoles("business")
    public MyResult findGoodsByUserId(@PathVariable("uId") Integer uId) {
        List<Shopcar> shopcarList = shopcarService.findGoodsByUserId(uId);
        return new MyResult(200, "根据用户id获取用户选中的商品列表",null,shopcarList,null,null);
    }

    /**
     * 根据用户id和商品id将此条购物车选中
     * @param uId
     * @param gId
     * @return
     */
    @RequestMapping("/updateCheckedByUidAndGid")
    @RequiresRoles("business")
    public MyResult updateCheckedByUidAndGid(@RequestParam("uId") Integer uId,
                                             @RequestParam("gId") Integer gId) {
        shopcarService.updateCheckedByUidAndGid(uId, gId);
        return new MyResult(200, "根据用户id和商品id将此条购物车选中",null,null,null,null);
    }

    /**
     * 根据用户id和商品id查询此条购物车
     * @param uId
     * @param gId
     * @return
     */
    @RequestMapping("/findByGidAndUid")
    @RequiresRoles("business")
    public MyResult findByGidAndUid(@RequestParam("uId") Integer uId,
                                    @RequestParam("gId") Integer gId) {
        Shopcar shopcar = shopcarService.findByGidAndUid(uId, gId);
        return new MyResult(200, "根据用户id和商品id查询此条购物车",null,null,shopcar,null);
    }

}
